<?php
namespace App\Service\Admin\Ywmall;

class FuwuService extends BaseService
{
    //服务

    //列表
    public static function index()
    {
        $db = self::dbConn(self::$dbYwFuwu)->where('del', 0);
        $total = $db->count();
        $res = $db->orderBy('id', 'asc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        foreach ($res as &$v) {
            $v->cover = file_exists($v->cover) ? $v->cover : '';
            $v->price = toNumber($v->price);
        }
        return self::toback(0, '', $res, $total);
    }

    /**
     * 新增
     * @param array $pas
     */
    public static function add($pas)
    {
        $pas['uptime'] = $pas['addtime'] = toDate();
        $res = self::dbConn(self::$dbYwFuwu)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     */
    public static function modify($id, $pas)
    {
        $pas['uptime'] = toDate();
        $up = self::dbConn(self::$dbYwFuwu)
            ->where('id', $id)
            ->update($pas);
        if (!$up) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * 删除
     * @param id $id 场景ID
     * return array
     */
    public static function del($id)
    {
        self::dbConn(self::$dbYwFuwu)
            ->where('id', $id)
            ->update(['del' => 1]);
        return self::toback();
    }
}